package bscorp.appbase.activity;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import bscorp.appbase.R;
import bscorp.appbase.bmo.DeviceBmo;
import bscorp.appbase.model.Device;

public class AddDeviceActivity extends AppCompatActivity {
    EditText edit_nome, edit_code, edit_department, edit_location, edit_producer, edit_supplier;
    Button btn_add;
    private boolean isAdd;

    private Device mDevice;
    private AddDeviceTask mTask = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_device);
        edit_nome = (EditText) findViewById(R.id.edit_nome);
        edit_code = (EditText) findViewById(R.id.edit_code);
        edit_department = (EditText) findViewById(R.id.edit_department);
        edit_location = (EditText) findViewById(R.id.edit_location);
        edit_producer = (EditText) findViewById(R.id.edit_producer);
        edit_supplier = (EditText) findViewById(R.id.edit_supplier);
        btn_add = (Button) findViewById(R.id.btn_add);
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                attemptCreate();
            }
        });

        if (getIntent().hasExtra("device")) {
            mDevice = (Device) getIntent().getSerializableExtra("device");
            edit_nome.setText(mDevice.getName());
            edit_code.setText(mDevice.getCode());
            edit_department.setText(mDevice.getDepartment());
            edit_location.setText(mDevice.getLocation());
            edit_producer.setText(mDevice.getManufacturer());
            edit_supplier.setText(mDevice.getSupplier());
            isAdd = false;
        } else {
            mDevice = new Device();
            isAdd = true;
        }
    }

    public void attemptCreate() {
        // Store values at the time of the login attempt.
        String nome = edit_nome.getText().toString();
        String code = edit_code.getText().toString();
        String department = edit_department.getText().toString();
        String location = edit_location.getText().toString();
        String producer = edit_producer.getText().toString();
        String supplier = edit_supplier.getText().toString();

        boolean cancel = false;
        View focusView = null;

        // Check for a valid email address.
        if (TextUtils.isEmpty(nome)) {
            edit_nome.setError(getString(R.string.error_field_required));
            focusView = edit_nome;
            cancel = true;
        } else if (TextUtils.isEmpty(code)) {
            edit_code.setError(getString(R.string.error_field_required));
            focusView = edit_code;
            cancel = true;
        } else if (TextUtils.isEmpty(department)) {
            edit_department.setError(getString(R.string.error_field_required));
            focusView = edit_department;
            cancel = true;
        } else if (TextUtils.isEmpty(location)) {
            edit_location.setError(getString(R.string.error_field_required));
            focusView = edit_location;
            cancel = true;
        } else if (TextUtils.isEmpty(producer)) {
            edit_producer.setError(getString(R.string.error_field_required));
            focusView = edit_producer;
            cancel = true;
        } else if (TextUtils.isEmpty(supplier)) {
            edit_supplier.setError(getString(R.string.error_field_required));
            focusView = edit_supplier;
            cancel = true;
        }

        if (cancel) {
            focusView.requestFocus();
        } else {
            mDevice.setName(nome);
            mDevice.setCode(code);
            mDevice.setDepartment(department);
            mDevice.setLocation(location);
            mDevice.setManufacturer(producer);
            mDevice.setSupplier(supplier);
            mTask = new AddDeviceTask(mDevice);
            mTask.execute((Void) null);
        }
    }

    public class AddDeviceTask extends AsyncTask<Void, Void, Boolean> {
        private final Device aDevice;


        AddDeviceTask(Device device) {
            aDevice = device;
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            DeviceBmo deviceBmo = new DeviceBmo();
            boolean resule;
            if (isAdd) {
                resule = deviceBmo.saveDevice(aDevice);
            } else {
                resule = deviceBmo.updateDevice(aDevice);
            }
            return resule;
        }

        @Override
        protected void onPostExecute(final Boolean success) {
            mTask = null;
            if (success) {
                Toast.makeText(AddDeviceActivity.this, "成功", Toast.LENGTH_SHORT).show();
                onBackPressed();
            } else {
                Toast.makeText(AddDeviceActivity.this, "失败", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        protected void onCancelled() {
            mTask = null;
        }
    }

    @Override
    public void onBackPressed() {
        finish();
    }
}
